#include "dungeonfd/global.h"
#include "lvledit/lvledit.h"
#include "lvledit/lvledit_validator.h"

#include "lvledit/lvledit_actions.h"
#include "lvledit/lvledit_display.h"
#include "lvledit/lvledit_grass_actions.h"
#include "lvledit/lvledit_map.h"
#include "lvledit/lvledit_menu.h"
#include "lvledit/lvledit_tools.h"
#include "lvledit/lvledit_widgets.h"

struct mapgen_gamelevel vizmap;

static unsigned char hasgift[300];

static level *target_level;

unsigned char viz_has_gift(int r)
{
	return hasgift[r];
}

void set_dungeon_output(level *output)
{
	target_level = output;
}

void fd_convert(int w, int h, unsigned char *tiles, int *rooms)
{
	int y, x;
	
	for (y = 0; y < h; y++) {
		for (x = 0; x < w; x++) {
			switch (tiles[y*w+x]) {
				case 4:
					action_create_obstacle(target_level, x + 0.5, y + 1, 2);
					action_set_floor(target_level, x, y, 0);
					break;
				case 5:
					action_create_obstacle(target_level, x + 0.5, y, 2);
					action_set_floor(target_level, x, y, 0);
					break;
				case 6:
					action_create_obstacle(target_level, x + 1.0, y + 0.5, 1);
					action_set_floor(target_level, x, y, 0);
					break;
				case 7:
					action_create_obstacle(target_level, x, y + 0.5, 1);
					action_set_floor(target_level, x, y, 0);
					break;
				case 11:
					action_create_obstacle(target_level, x + 0.5, y, 2);
					action_create_obstacle(target_level, x, y + 0.5, 1);
					action_set_floor(target_level, x, y, 0);
					break;
				case 10:
					action_create_obstacle(target_level, x + 0.5, y, 2);
					action_create_obstacle(target_level, x + 1, y + 0.5, 1);
					action_set_floor(target_level, x, y, 0);
					break;
				case 9:
					action_create_obstacle(target_level, x, y + 0.5, 1);
					action_create_obstacle(target_level, x + 0.5, y + 1, 2);
					action_set_floor(target_level, x, y, 0);
					break;
				case 8:
					action_create_obstacle(target_level, x + 1, y + 0.5, 1);
					action_set_floor(target_level, x, y, 0);
					action_create_obstacle(target_level, x + 0.5, y + 1, 2);
					break;

				case 12:
				case 0:
				case 1:
				case 2:
				case 3:
					action_set_floor(target_level, x, y, 0);
					break;

				case 17:
					action_set_floor(target_level, x, y, 31);
					break;

				case 16:
					action_set_floor(target_level, x, y, 56);
				    break;	

				case 15:
					action_create_obstacle(target_level, x + 1, y + 0.5, 11);
					action_set_floor(target_level, x, y, 58);
				    break;	
				
				case 14:
					action_create_obstacle(target_level, x + 0.5, y, 6);
					action_set_floor(target_level, x, y, 57);
				    break;	

				case 13:
					action_set_floor(target_level, x, y, 59);
				    break;	

				default:
					action_set_floor(target_level, x, y, tiles[y*w+x]);
			}
		}
	}

}

void viz_entry_at(roominfo *r)
{
	action_create_obstacle(target_level, r->x + r->w / 2, r->y + r->h / 2, 16);
}

void viz_exit_at(roominfo *r)
{
	action_create_obstacle(target_level, r->x + r->w / 2, r->y + r->h / 2, 16);
}

void viz_place_enemies(roominfo *r)
{
	(void) r;
}

void viz_gift(roominfo *r)
{
	printf("Adding gift in room %d\n", (r-&rooms[0]));
	hasgift[r-&rooms[0]] = 1;
}

void viz_teleporter(roominfo *from, roominfo *to)
{
	(void) from;
	(void) to;
}

void fd_init_hooks()
{
	dungeonmap_convert = fd_convert;
	dungeonmap_entry_at = viz_entry_at;
	dungeonmap_exit_at = viz_exit_at;
	dungeonmap_place_enemies = viz_place_enemies;
	dungeonmap_gift = viz_gift;
}


